07. What is a Contract Class?

What is a Contract Class?

Question:

Start Quiz:

Solution:

We just covered the structure of the Contract class. These are the 3 components for a general Contract (Note: In the example below, "Blank" can be replaced by a relevant name. For example, for BlankContract it would be WeatherContract)

  1. Outer class name BlankContract (e.g. WeatherContract)
  2. Inner class name BlankEntry (e.g. LocationEntry) for each table in the database. Each of these inner classes should implement a class called BaseColumns
  3. String constants for table name and for each of the readings (e.g. TABLE_NAME, COLUMN_CITY_NAME, etc).

A contract class should have 3 parts

A contract class should have 3 parts

Use this gist with the WeatherContract from the Sunshine app

(Optional): Here is the full codebase for Sunshine if you are interested in exploring where the WeatherContract came from.

Additional Examples:
The Contacts app from Google contains a SQLite database to store all of your contacts’ information. The app also defines a ContactsContract for the table and column names in the contacts database. See Contacts table columns.

The Calendar app from Google also contains a SQLite database to store all your calendar entries. The app defines a CalendarContract for the table and column names in the calendar database. For example, check out the columns for the Events table.